<template>
<div v-if="loadPage">
    <!-- 开通e权限协议 -->
    <div class="body" ref="eRightContract"></div>

    <div class="footer-box">
      <div class="footer fix">
        <a href="javascript:;" class="btn" @click="agree">同意签署</a>
      </div>
    </div>
</div>
</template>
<script>
import {
  getUserRights, signEContract, getContractById, signWithContent
} from '@/api/index';
import DZUtils from '@/utils/DZUtils';

export default {
  components: {
  },
  data() {
    return {
      loadPage: false,
      redirectUrl: '',
      redirectParam: '',
      id: '', // 协议id
      html: '' // 协议内容
    };
  },
  mounted() {
    const redirect = DZUtils.getStorage('redirect');
    this.redirectUrl = redirect.redirectUrl;
    this.redirectParam = redirect.redirectParam;
    this.loadData();
  },
  methods: {
    async loadData() {
      const { returnCode, dataSet, returnInfo } = await getUserRights();
      if (returnCode === '0' && dataSet.length > 0) {
        const hasEright = dataSet[0].client_rights.indexOf('e') > -1;
        // e权限未开通
        if (!hasEright) {
          this.loadPage = true;
          const res2 = await getContractById({ type: 'eRightCt' });
          if (res2.returnCode === '0') {
            this.id = res2.dataSet[0].id;
            this.html = res2.dataSet[0].templateContent;

            this.$nextTick(() => {
              this.$refs.eRightContract.innerHTML = this.html;
            });
          }
        } else {
          // e权限已开通
          this.goBack();
        }
      } else {
        DZUtils.iAlert('错误提示', returnInfo);
      }
    },
    async agree() {
      const batchNo = DZUtils.getStorage('batchNo') || '';
      const params = {
        id: this.id,
        batchNo,
        content: this.html.replace(/<[^>]+>/g, ''),
        source: 'eRightContract'
      };
      const { returnCode, dataSet } = await signWithContent(params);
      // 柜台留痕，留痕成功；
      if (returnCode === '0') {
        console.log('协议中心留痕，留痕成功');
      }
      const res2 = await signEContract();
      if (res2.returnCode === '0') {
        // 开通e权限
        this.goBack();
      } else {
        DZUtils.iAlert('错误提示', res2.returnInfo);
      }
    },
    goBack() {
      this.$router.replace({
        path: this.redirectUrl,
        query: {
          ...this.redirectParam,
          prevPage: 'eRightContract'
        }
      });
    }
  }
};
</script>
<style lang="less" scoped>
.body{
  padding: 10px 15px 60px;
  background: #fff;
  overflow-y: auto;
  overflow-x: hidden;
  position: relative;
}
.footer-box{
  height: 50px;
  .footer{
    width: 100%;
    text-align: center;
    &.fix{
      position: fixed;
      left: 0;
      bottom: 0;
      z-index: 1;
    }
    .btn {
      width: 100%;
      font-size: 18px;
      color: #fff;
      text-align: center;
      line-height: 50px;
      display: inline-block;
      background-image: linear-gradient(90deg, #3861FE 0%, #489BFE 100%);
      &[disabled=disabled]{
        color: #72819B;
        background-image: linear-gradient(90deg, #B5C6E5 0%, #E2E8F2 100%);
      }
    }
  }
}
</style>
